
import 'package:flutter/material.dart';
import 'package:flutter_app/util/catch.dart';

class LoginPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _loginState();
  }

}
class _loginState extends State{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
    String _name;
    String _password;
    final cach = new CachService();
    _forSubmitted() {
      var _form = _formKey.currentState;

      if (_form.validate()) {
        _form.save();
        cach.setMsg('__isYk__', '1');
        cach.setMsg('__token__', _name+'='+_password);
        Navigator.pushNamedAndRemoveUntil(this.context, '/',(Route<dynamic> route) => false);
      }
    }
    _youkeSubmit(){
      cach.setMsg('__isYk__', '0');
      //第三个参数返回false 就会一直删除路由栈内的所有路由 直到返回true
      Navigator.pushNamedAndRemoveUntil(this.context, '/',(Route<dynamic> route) => false);
    }

    return new Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("static/images/bg.jpg"),
            fit: BoxFit.cover,
          ),
        ),
            child: Center(
              child: Container(
                width: 300,
                height:300,
                color: Colors.transparent,
                child: new Form(
                  key: _formKey,
                  child: new Column(
                    children: <Widget>[
                      new TextFormField(
                          decoration: new InputDecoration(
                              labelText: '用户名',
                              focusedBorder:  UnderlineInputBorder(
                                  borderSide: BorderSide(
                                      color: Colors.lightBlue
                                  )
                              )
                          ),
                          onSaved: (val) {
                            _name = val;
                          }
                      ),
                      new TextFormField(
                        decoration: new InputDecoration(
                          labelText: '密码',
                          focusedBorder:  UnderlineInputBorder(
                            borderSide: BorderSide(
                              color: Colors.lightBlue
                            )
                          )
                        ),
                        obscureText: true,
                        validator: (val) {
                          RegExp exp = RegExp(r'^[a-zA-Z0-9]{6,12}$');
                          return exp.hasMatch(val) ? null : "密码数字或者字母同时6-12位";
                        },
                        onSaved: (val) {
                          _password = val;
                        },
                      ),
                      Container(
                        margin: EdgeInsetsDirectional.fromSTEB(0, 20, 0, 0),
                        child:Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children:<Widget>[
                            new MaterialButton (
                                color: Colors.blue,
                                textColor: Colors.white,
                                minWidth: 120,
                                child: Text('登录'),
                                onPressed: _forSubmitted
                            ),
                            new MaterialButton (
                                color: Colors.transparent,
                                textColor: Colors.white,
                                minWidth: 120,
                                child: Text('游客'),
                                onPressed: _youkeSubmit
                            ),
                            ]
                        )
                      )
                    ],
                  ),
                ),
              ),
            ),
          )
      );
  }
}